import fly from '../request'
import jsonxml from 'jsontoxml'
import xml2json from 'xmlstring2json'
import { isTest, baseHost } from '../config'
import wx from '../wx'

let host =
	'http://cs.fybj365.com:18001/fybj/services/ChildDataByCardAndNaService?wsdl'

let childrenPost = (api, params) => {
	let resultArray = []
	Object.entries(params).forEach(item => {
		resultArray.push({
			name: item[0],
			text: item[1]
		})
	})
	let obj = [
		{
			name: api,
			attrs: { xmlns: 'urn:ihe:iti:xds-b:2007' },
			children: [
				{
					DocumentRequest: resultArray
				}
			]
		}
	]
	let xml = jsonxml(params)
	let soap = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:heal="http://healthcard.webservice.child.main.code/"><soapenv:Header/><soapenv:Body><heal:getChildList>${xml}</heal:getChildList></soapenv:Body></soapenv:Envelope>`
	return new Promise((resolve, reject) => {
		fly
			.post(host, soap, {
				headers: {
					'Content-Type': 'text/xml; charset=utf-8'
				}
			})
			.then(response => {
				resolve(
					xml2json(response)['soap:Envelope']['soap:Body'][
						'ns2:getChildListResponse'
					]['childDataResponse']['checkDatas']
				)
			})
			.catch(error => {
				wx.showToast({
					title: error
				})
				reject(error)
			})
	})
}

let getChildList = params => {
	return childrenPost('getChildList', params)
}

export default {
	getChildList // 查询小孩信息
}
